/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.clientManagement.managedbean;

import crm.clientManagement.entity.AddressEntity;
import crm.clientManagement.entity.CompanyEntity;
import crm.clientManagement.session.CompanySessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class EditClientManagedBean implements Serializable{

    /**
     * @return the logger
     */
    public static Logger getLogger() {
        return logger;
    }

    /**
     * @param aLogger the logger to set
     */
    public static void setLogger(Logger aLogger) {
        logger = aLogger;
    }
    
    @EJB
    CompanySessionBean companySessionBean;
    
    private CompanyEntity salesLead;
    private CompanyEntity customer;
    
    private static Logger logger = Logger.getLogger(EditClientManagedBean.class.getName());
    
    private AddressEntity address = new AddressEntity();

    /** Creates a new instance of EditClientManagedBean */
    public EditClientManagedBean() {
    }
    
    public void salesLeadInit(PhaseEvent event) {

        if (getSalesLead() == null) {

            setSalesLead((CompanyEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("salesLead"));
        }
        
    }
    
    public void customerInit(PhaseEvent event) {

        if (getCustomer() == null) {

            setCustomer((CompanyEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("customer"));
        }
    }
    
    public void salesLeadSaveChanges(){
        
        companySessionBean.saveChanges(getSalesLead());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Changes saved.", ""));        
    }
    
    public void customerSaveChanges(ActionEvent event){
        companySessionBean.saveChanges(getCustomer());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Changes saved.", ""));        
    }
    
    public void goBack() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteCompany.xhtml");
    }
    
    public void convert() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("converting", salesLead);
        FacesContext.getCurrentInstance().getExternalContext().redirect("./convertToCustomer.xhtml");
    }
    
    public void deleteBilling(AddressEntity selectedBilling){
        for (int i=0;i<customer.getBillingAddresses().size();i++){
            if (customer.getBillingAddresses().get(i).getStreetName().equals(selectedBilling.getStreetName())){
                customer.getBillingAddresses().remove(i);
                return;
            }
        }
    }
    
    public void addToBilling(ActionEvent event){
        
        if (address.getStreetName().equals("")||address.getCity().equals("")||address.getPostCode().equals("")){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Please fill in all fields", ""));
            return;
        }
        else{
            customer.getBillingAddresses().add(address);
            address = new AddressEntity();
        }
    }
    
    public void deleteShipping(AddressEntity selectedShipping){
        for (int i=0;i<customer.getShippingAddresses().size();i++){
            if (customer.getShippingAddresses().get(i).getStreetName().equals(selectedShipping.getStreetName())){
                customer.getShippingAddresses().remove(i);
                return;
            }
        }
    }
    
    public void addToShipping(ActionEvent event){
        
        if (address.getStreetName().equals("")||address.getCity().equals("")||address.getPostCode().equals("")){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Please fill in all fields", ""));
            return;
        }
        else{
            customer.getShippingAddresses().add(address);
            address = new AddressEntity();
        }
    }
    
    public String onFlowProcess(FlowEvent event) {  
        getLogger().info("Current wizard step:" + event.getOldStep());  
        getLogger().info("Next step:" + event.getNewStep());
        
        return event.getNewStep();  
       
    }

    /**
     * @return the salesLead
     */
    public CompanyEntity getSalesLead() {
        return salesLead;
    }

    /**
     * @param salesLead the salesLead to set
     */
    public void setSalesLead(CompanyEntity salesLead) {
        this.salesLead = salesLead;
    }

    /**
     * @return the customer
     */
    public CompanyEntity getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(CompanyEntity customer) {
        this.customer = customer;
    }


    /**
     * @return the address
     */
    public AddressEntity getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(AddressEntity address) {
        this.address = address;
    }
}
